package com.huanxun.utils;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

/**
 * 缓存工具类
 *
 */ 
public final class CacheUtils {
	
	public static void saveJsonData(Context context,String url,String json) throws IOException{
		String FileName = Md5Utils.encode(url)+".json";
		FileOutputStream openFileOutput = context.openFileOutput(FileName, Context.MODE_PRIVATE);
		openFileOutput.write(json.getBytes());
		openFileOutput.close();
	}
	
	public static String readJsonData(Context context,String url) throws IOException{
		String FileName = Md5Utils.encode(url)+".json";
		FileInputStream openFileInput = context.openFileInput(FileName);
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		byte[] buffer =new byte[1024];
		int len =0;
		while ((len=openFileInput.read(buffer))!=-1){
			byteArrayOutputStream.write(buffer, 0, len);
		}
		String json = byteArrayOutputStream.toString();
		byteArrayOutputStream.close();
		openFileInput.close();
		return json;
		
	}
}
